package org.mobai.flow.camundaListener;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.mobai.flow.constant.CamundaConstant;
import org.mobai.flow.enums.BpmnBaseVarEnum;
import org.springframework.stereotype.Component;

/**
 * @author mobai
 */
@Component("multiInstanceCompleteTask")
@Slf4j
public class MultiInstanceCompleteTask {
    public Boolean completeTask(DelegateExecution execution, Double d) {
        Object nrOfInstances = execution.getVariable("nrOfInstances");
        Object nrOfActiveInstances = execution.getVariable("nrOfActiveInstances");
        Object nrOfCompletedInstances = execution.getVariable("nrOfCompletedInstances");
        String activityInstanceId = execution.getActivityInstanceId();
        log.info("{}--->总会签数量：{}", activityInstanceId, nrOfInstances);
        log.info("{}--->当前获取的会签数量：{}", activityInstanceId, nrOfActiveInstances);
        log.info("{}--->已经完成的会签数量：{}", activityInstanceId, nrOfCompletedInstances);
        double v = Double.parseDouble(nrOfCompletedInstances.toString()) / Double.parseDouble(nrOfInstances.toString());
        boolean flag = (v >= d / 100);
        log.info("{}--->当前已经完成的会签数量/总会签数量：{}", activityInstanceId, v);
        log.info("{}--->当前是是否通过：{}", activityInstanceId, flag);
        Object variable = execution.getVariable(BpmnBaseVarEnum.MOBAI_FLOW_STATUS.getType());
        if (variable != null && ObjectUtil.equal(CamundaConstant.FLOW_BACK, variable.toString())) {
            log.info("{}--->当前流程被驳回，直接结束", activityInstanceId);
            return true;
        }
        return flag;

    }
}
